const koa=require('koa');
const Router=require('@koa/router');
const {koaBody}=require('koa-body');
const cors=require('koa2-cors');
const db=require('./db');
const jwt=require('jsonwebtoken');
const KEY= '12376534fddf5434gfdfg';


const app=new koa();
const router=new Router();
db.init()

app.use(cors());
app.use(koaBody());
app.use(router.routes());



router.post('/login',async(ctx,next)=>{
    const obj=ctx.request.body;
    const arr= await db.getLogin(obj) 
    let token=jwt.sign({username:obj.username},KEY)
    if(arr.length==0){
      ctx.body={
        code:100,
        msg:'用户名或密码错误'
      }
    }else if(arr.length>0){
      if(arr[0].username==obj.username&&arr[0].password==obj.password){
        ctx.body={
          code:200,
          msg:'登录成功',
          token
        }
        
      }
    }
})

router.post('/changeTab',async(ctx,next)=>{
  const {type}=ctx.request.body;
  const arr= await db.changeTab(type) 
  ctx.body=arr
})

router.get('/getfloorLeft',async(ctx,next)=>{
  const arr= await db.getfloorLeft() 
  ctx.body=arr
})

router.get('/getfloorRight',async(ctx,next)=>{
  const arr= await db.getfloorRight() 
  ctx.body=arr
})



app.listen(8000,()=>{
  console.log('服务器启动成功端口：《http://localhost:8000/》');
})


